#include <iostream>
#include <sys/types.h>
#include <sys/msg.h>
#include <unistd.h>
#include <errno.h>

using namespace std;


typedef struct
{
    long type; //类型
    int start; //消息队列本身
    int end;
} msg;


int main(int argc,char*argv[]){


    if(argc<3){
        printf("usage:%s key type\n",argv[0]);

        exit(1);
    }
    //获取key
    key_t key=atoi(argv[1]);
    long type=atoi(argv[2]);
    int msq_id;
    if((msq_id=msgget(key,0777))<0){
        std::cout<<"msqid error"<<std::endl;

        exit(1);
    }
    //从消息队列种接受指定类型的消息
    msg m;
    if(msgrcv(msq_id,&m,sizeof(msg)-sizeof(long),type,IPC_NOWAIT)<0){
        perror("msqrcv error");
    }else{
        std::cout<<"msq message"<<m.type<<" "<<m.start<<" "<<m.end<<std::endl;

    }


    return 0;
}


